package cn.b.sky.flowable.listener;

import org.flowable.bpmn.model.*;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.engine.impl.bpmn.parser.BpmnParse;
import org.flowable.engine.impl.bpmn.parser.handler.UserTaskParseHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

/**
 * 
 * <strong>Title : ProxyUserTaskBpmnParseHandler.java<br></strong>
 * <strong>Package : cn.b.sky.flowable.listener<br></strong>
 * <strong>Description : </strong>委托模式重写userTask处理器<br> 
 * <strong>Create on : 2016-5-5 下午3:16:03<br></strong>
 * <p>
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
public class ProxyUserTaskBpmnParseHandler extends UserTaskParseHandler {
  
  private static final Logger log= LoggerFactory.getLogger(ProxyUserTaskBpmnParseHandler.class);

  private String taskListenerId;

  
  @Override
  public void executeParse(BpmnParse bpmnParse, UserTask userTask) {
    
    log.debug("bpmnParse : {}, userTask : {}", bpmnParse, userTask);

    userTask.getTaskListeners().add(this.configEvent(TaskListener.EVENTNAME_CREATE));
    userTask.getTaskListeners().add(this.configEvent(TaskListener.EVENTNAME_COMPLETE));
    //清空任务的人员配置，以系统的的配置为准
    userTask.setAssignee(null);
    userTask.setCandidateUsers(new ArrayList<String>());
    userTask.setCandidateGroups(new ArrayList<String>());
    super.executeParse(bpmnParse, userTask);
  }

  public FlowableListener configEvent(String eventName) {
    FlowableListener activitiListener = new FlowableListener();
    activitiListener.setEvent(eventName);
    activitiListener.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION);
    activitiListener.setImplementation("#{" + taskListenerId + "}");
    return activitiListener;
  }

  @Override
  public Class< ? extends BaseElement> getHandledType() {
    return UserTask.class;
  }
  
  public void setTaskListenerId(String taskListenerId) {
    this.taskListenerId = taskListenerId;
  }

}
